package com.bwstudio.demo.provider.controller;

import com.bwstudio.demo.provider.model.AccountChangeEvent;
import com.bwstudio.demo.provider.service.AccountInfoService;
import com.bwstudio.demo.provider.service.NewAccountInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.UUID;

@RestController
@RequestMapping(value = "/new")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NewAccountInfoController {

	private final NewAccountInfoService newAccountInfoService;

	/**
	 * @Description: 转账demo api
	 * @param accountFrom 转出方账号
	 * @param amount 转出金额
	 * @return "转账成功"
	 * @Author: Bu Wei
	 * @Date: 2020年3月11日 上午10:33:05
	 */
	@GetMapping(value = "/transfer")
	public String transfer(@RequestParam("accountNo") String accountFrom, @RequestParam("amount") BigDecimal amount) {
		// 创建一个事务id，作为消息内容发到mq
		String tx_no = UUID.randomUUID().toString();
		AccountChangeEvent accountChangeEvent = new AccountChangeEvent(accountFrom, amount, tx_no);
		newAccountInfoService.transfer(accountChangeEvent);
		return "转账成功";
	}

}
